package com.dayuanit.dy8.mall.springbootmall.service.impl;

import com.dayuanit.dy8.mall.springbootmall.dto.CatalogDTO3;
import com.dayuanit.dy8.mall.springbootmall.dto.HomeCommonDTO;
import com.dayuanit.dy8.mall.springbootmall.entity.Catalog;
import com.dayuanit.dy8.mall.springbootmall.entity.Commodity;
import com.dayuanit.dy8.mall.springbootmall.mapper.CatalogMapper;
import com.dayuanit.dy8.mall.springbootmall.mapper.CommodityMapper;
import com.dayuanit.dy8.mall.springbootmall.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private CatalogMapper catalogMapper;

    @Autowired
    private CommodityMapper commodityMapper;

    @Value("${image.url}")
    private String imageUrl;


    public List<CatalogDTO3> listCatalog() {

        final List<Catalog> firstCatalogs = catalogMapper.listFirst();
        final List<Catalog> secondCatalogs = catalogMapper.secondFirst();

        List<CatalogDTO3> dtoList = new ArrayList<>(firstCatalogs.size());
        for (Catalog catalog : firstCatalogs) {
            final CatalogDTO3 dto = new CatalogDTO3();
            dtoList.add(dto);

            dto.setCataCode(catalog.getCatalogNum());
            dto.setCataName(catalog.getCatalogName());
            dto.setParentCode(catalog.getParentNum());

            List<CatalogDTO3> secondDTOs = new ArrayList<>();
            dto.setChilds(secondDTOs);

            for (Catalog sedLog : secondCatalogs) {
                if (!sedLog.getParentNum().equals(catalog.getCatalogNum())) {
                    continue;
                }

                final CatalogDTO3 secondDTO = new CatalogDTO3();
                secondDTOs.add(secondDTO);

                secondDTO.setParentCode(sedLog.getParentNum());
                secondDTO.setCataName(sedLog.getCatalogName());
                secondDTO.setCataCode(sedLog.getCatalogNum());
            }
        }

        return dtoList;
    }

    public List<HomeCommonDTO> listHomeCommon() {
        //获取所有的一级目录
        final List<Catalog> catalogs = catalogMapper.listFirst();

        List<HomeCommonDTO> dtoListt = new ArrayList<>(catalogs.size());

        for (Catalog firstCatalog : catalogs) {

            HomeCommonDTO homeDTO = new HomeCommonDTO();
            dtoListt.add(homeDTO);

            homeDTO.setCatalogName(firstCatalog.getCatalogName());

            List<HomeCommonDTO.CommonInfo> commonInfoList = new ArrayList<>();
            homeDTO.setCommons(commonInfoList);

            //再获取每个一级目录的商品 12商品
            final List<Commodity> commodities = commodityMapper.listHome(firstCatalog.getCatalogNum());
            for (Commodity commodity : commodities) {
                HomeCommonDTO.CommonInfo commonInfo = new HomeCommonDTO.CommonInfo();
                commonInfoList.add(commonInfo);

                commonInfo.setCommonId(commodity.getId());
                commonInfo.setCommonName(commodity.getCommoName());
                commonInfo.setPicUrl(imageUrl + commodity.getPictureUrl());
                commonInfo.setPrice(String.valueOf(commodity.getPrice()));
            }
        }

        return dtoListt;
    }
}
